﻿using System;

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Collections;
using System.ComponentModel;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using App2Wpf.Views;
using System.Collections.Generic;


namespace App2Wpf.ViewModels
{
    public class RightWrapVM :ObservableObject
    {
        public int id;

        private ObservableCollection<Button> _controlsBtn = new ObservableCollection<Button>();
        public ObservableCollection<Button> ControlsBtn
        {
            get { return _controlsBtn; }
            set
            {
                _controlsBtn = value;
                //OnPropertyChanged("Controls");
                SetProperty(ref _controlsBtn, value);
            }
        }

        public void AddControlBtn(nameip obj)
        {
            // 设置控件的属性、样式等
            Button btn1 = new Button
            {
                Tag = 1,
                Content = "this add",
                Width = 300,
                Height = 50
            };
            ControlsBtn.Add(btn1);

        }

        private ObservableCollection<CardTest> _controlsCard = new ObservableCollection<CardTest>();
        public ObservableCollection<CardTest> ControlsCard
        {
            get { return _controlsCard; }
            set
            {
                _controlsCard = value;
                //OnPropertyChanged("Controls");
                SetProperty(ref _controlsCard, value);
            }
        }

        /// <summary>
        /// FormHostPicture
        /// </summary>
        private ObservableCollection<FormUC> _controlsFormPic = new ObservableCollection<FormUC>();
        public ObservableCollection<FormUC> controlsFormPic
        {
            get { return _controlsFormPic; }
            set
            {
                _controlsFormPic = value;
                //OnPropertyChanged("Controls");
                SetProperty(ref _controlsFormPic, value);
            }
        }

        private ObservableCollection<ICardUC> _cards = new ObservableCollection<ICardUC>();
        public ObservableCollection<ICardUC> cards
        {
            get { return _cards; }
            set
            {
                _cards = value;
                SetProperty(ref _cards, value);
            }
        }

        public void AddCard(nameip obj)
        {
            ICardUC userControl;
            switch (ServiceLocator.firstVM.ChooseCardMode)
            {
                case RenderingMode.WritePixels:
                    userControl = new WritePixelsCard(obj);
                    break;
                case RenderingMode.PictureBox:
                    userControl = new PictureBoxUC(obj);
                    break;
                case RenderingMode.FormEmbed:
                    userControl = new FormUC(obj);
                    break;
                

                case RenderingMode.ExBlitCard:
                    userControl = new ExBlitCard(obj);
                    break;

                case RenderingMode.Canvas:
                    userControl = new CanvasCardUC(obj);
                    break;

                case RenderingMode.DrawVisual:
                case RenderingMode.CardVMTest:
                case RenderingMode.DrawingGroup:
                case RenderingMode.Unsafe:
                case RenderingMode.render3:
                    userControl = new CardTest(obj);
                    break;

                default:
                    userControl = new PictureBoxUC(obj);
                    break;
            }
            userControl.ID = id++;
            userControl.StartWork();
            cards.Add(userControl);
            
            // 设置控件的属性、样式等
        }

        public void AddCardAsyncTask(nameip obj)
        {
            //调用线程必须为 STA，因为许多 UI 组件都需要。用Dispatcher
            App.Current.Dispatcher.BeginInvoke(new Action(
                () => AddCard(obj) ));
        }

        public void removeCard(ICardUC vcard)
        {
            List<ICardUC> temp = cards.Where(p=> p.ID == vcard.ID).ToList();
            if(temp.Count >0) cards.Remove(temp.FirstOrDefault());

        }
        /// <summary>
        /// 未实现ICardUC接口的 VM调用
        /// </summary>
        public void removeCardById(int id)
        {
            List<ICardUC> temp = cards.Where(p => p.ID == id).ToList();
            if (temp.Count > 0) cards.Remove(temp.FirstOrDefault());

        }
    }
}
